<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="群名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入群名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请选择成员">
        <el-popover
          placement="bottom-start"
          width="520"
          trigger="click"
          @show="$refs['employeeSelect'].reset()"
        >
          <EmployeeSelect ref="employeeSelect" @selectedEmployees="selectedEmployees"/>
          <el-input slot="reference" v-model="form.icon" placeholder="点击选择成员" readonly>
            <el-tag style="margin-right: 5px;margin-bottom: 5px;"
                    v-for="employee in selectedEmployeeList"
                    :key="employee.name"
                    closable
                    @close="employeeRemove(employee)"
            >
              {{employee.name}}
            </el-tag>
          </el-input>
        </el-popover>
      </el-form-item>

      <el-form-item label="群公告" prop="notice">
        <el-input
          v-model="queryParams.notice"
          placeholder="请输入群公告"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择客户群状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="群创建时间" prop="createAt">
        <el-date-picker clearable size="small"
                        v-model="queryParams.createAt"
                        type="daterange"
                        range-separator="至"
                        unlink-panels
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh-right"
          size="mini"
          :loading="syncing"
          @click="handleSyncAll"
          v-hasPermi="['wework:groupchat:syncAll']"
        >同步所有群</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupchatList" @selection-change="handleSelectionChange"  @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组id" align="center" prop="id" v-if="false"/>
      <el-table-column label="群名称" align="left" width="200">
        <template slot-scope="scope">
<!--          <el-avatar shape="square" :size="40" :src="scope.row.thumbAvatar"></el-avatar>-->
          {{ scope.row.name }} <br>
          {{ scope.row.memberNum }}人<br>
          <el-tag size="mini">群主</el-tag>
          {{scope.row.owner.name}}
<!--          <i v-if="scope.row.gender=='1'" class="el-icon-male"></i>-->
<!--          <i v-if="scope.row.gender=='2'" class="el-icon-female"></i>-->

<!--          <el-tag v-if="scope.row.type=='1'" size="mini" type="success">@微信</el-tag>-->
<!--          <el-tag v-if="scope.row.type=='2'" size="mini" >@企业微信</el-tag>-->
        </template>
      </el-table-column>
      <el-table-column label="今日 入群/退群" align="center" width="120">
        <template slot-scope="scope">
          {{scope.row.joinNumToday}} / {{scope.row.quitNumToday}}
        </template>
      </el-table-column>
      <el-table-column label="群公告" align="left" prop="notice" width="300"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="管理员" align="center" >
      </el-table-column>
      <el-table-column label="群创建时间" align="center" prop="createAt" width="180" sortable="custom" :sort-orders="['descending', 'ascending']" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/groupchat/member/' + scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-user-solid">
              群成员列表
            </el-button>
          </router-link><br/>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleSync(scope.row)"
            v-hasPermi="['wework:groupchat:sync']"
          >同步更新群</el-button><br>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-pie-chart"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wework:groupchat:list']"
          >群统计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listGroupchat, syncAllGroupchat,syncGroupchat,getGroupchat, delGroupchat, updateGroupchat, exportGroupchat } from "@/api/wework/groupchat";
import EmployeeSelect from "@/components/EmployeeSelect";

export default {
  name: "Groupchat",
  components: {EmployeeSelect
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      //同步群...
      syncing: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户群列表表格数据
      groupchatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 客户群状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        ownerId: undefined,
        notice: undefined,
        status: undefined,
        createAt: undefined,
        categoryId: undefined,
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("groupchat_wx_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询客户群列表列表 */
    getList() {
      this.loading = true;
      listGroupchat(this.queryParams).then(response => {
        this.groupchatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 客户群状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    selectedEmployees(employees){
      console.log(employees);
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        corpId: undefined,
        wxChatId: undefined,
        name: undefined,
        ownerId: undefined,
        notice: undefined,
        status: 0,
        createAt: undefined,
        max: undefined,
        categoryId: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 同步所有群操作 */
    handleSyncAll() {
      this.syncing = true;
      syncAllGroupchat().then(()=>{
        this.syncing = false;
        this.getList()
      });
    },
    /** 同步指定群 */
    handleSync(row){
      this.syncing = true;
      syncGroupchat(row.wxChatId).then(()=>{
        this.syncing = false;
        this.getList()
      });
    }
  }
};
</script>
